package com.add.petback.controller;

import com.add.petback.model.*;
import com.add.petback.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
public class backController {
    @Autowired(required = false)
    private UserServiceImpl userService;
    @Autowired(required = false)
    private SellerServiceImpl sellerService;
    @Autowired(required = false)
    private PetServiceImpl petService;
    @Autowired(required = false)
    private CommentServiceImpl commentService;
    @Autowired(required = false)
    private ComplainmentServiceImpl complainService;


    @GetMapping(value = "/")
    public String index(Model model) {
        Seller seller = new Seller();
        model.addAttribute("seller",seller);
        return "index";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Seller seller) {
        System.out.println("卖家登录"+seller.getUsername()+seller.getPassword());
        Seller temp=sellerService.find(seller);
        if(temp==null)
            return"redirect:/";
        else
            return "redirect:/Homepage";
    }

    @RequestMapping(value = "/Homepage")
    public String Homepage(Model model) {

        return "Homepage";
    }

    @RequestMapping(value = "/Onsalegoods")
    public String onsalegoods(Model model) {
        List<Pet> pets = new ArrayList<>();
        List<Pet> foundPet = petService.getAll();
        Collections.reverse(foundPet);
        for (Pet newpet : foundPet) {
            if (newpet.getStatus().equals("在售"))
                pets.add(newpet);
        }
        model.addAttribute("pets", pets);
        return "Onsalegoods";
    }

    @RequestMapping(value = "/Usertable", method = RequestMethod.GET)
    public String list(Model model) {
        List<User> users = userService.getAll();
        Collections.reverse(users);
        model.addAttribute("users", users);
        return "Usertable";
    }

    @RequestMapping(value = "/Checkorder")
    public String checkorder(Model model) {
        List<Pet> pets = new ArrayList<>();
        List<Pet> foundPet = petService.getAll();
        Collections.reverse(foundPet);
        for (Pet newpet : foundPet) {
            if (!newpet.getStatus().equals("在售"))
                pets.add(newpet);
        }
        model.addAttribute("pets", pets);
        return "Checkorder";
    }

    @RequestMapping(value = "/Checkcomments")
    public String checkcomments(Model model) {
        List<Comment> comments = commentService.getAll();
        Collections.reverse(comments);
        model.addAttribute("comments", comments);
        return "Checkcomments";
    }

    @RequestMapping(value = "/Checkcomplain")
    public String checkcomplain(Model model) {
        List<Complainment> complains = complainService.getAll();
        Collections.reverse(complains);
        model.addAttribute("complains", complains);
        return "Checkcomplain";
    }

    // 添加商品页面
    @GetMapping(value = "/Addgoodpage")
    public String addgoodpage(Model model)  {
        Pet pet = new Pet();
        model.addAttribute("pet",pet);
        return "redirect:/Addgood";
    }

    //添加用户页面
    @GetMapping(value = "/Adduserpage")
    public String adduserpage(Model model)  {
        User user = new User();
        model.addAttribute("user",user);
        return "redirect:/Adduser";
    }

    // 更新商品页面
    @GetMapping(value = "/Updategood")
    public String updategood(String petpk,Model model) {
        Pet pet = new Pet();
        pet.setPetpk(Integer.parseInt(petpk));
        Pet found = petService.find(pet);
        model.addAttribute("pet",found);
        return "Updategood";
    }

    // 更新用户页面
    @GetMapping(value = "/Updateuser")
    public String updateuser(String userpk,Model model) {
        User user = new User();
        user.setUserpk(Integer.parseInt(userpk));
        User found = userService.find(user);
        model.addAttribute("user",found);
        return "Updateuser";
    }




    @RequestMapping(value = "/Deletegood", method = RequestMethod.GET)
    public String deletegood(String petpk) {
        Pet pet = new Pet();
        pet.setPetpk(Integer.parseInt(petpk));
        petService.delete(pet);
        return "redirect:/Onsalegoods";
    }

    // 更新商品函数
    @RequestMapping(value = "/Savegood", method = RequestMethod.POST)
    public String savegood(Pet pet) {
//        System.out.println("in");
//        System.out.println(pet.getPetpk());
//        System.out.println(pet.getName());
        petService.save(pet);
        return "redirect:/Onsalegoods";
    }

    // 添加商品的按钮的函数
    @PostMapping(value = "/Addgood")
    public String addgood(Pet pet)  {
        pet.setImage(new byte[0]);
        petService.insert(pet);
        return "redirect:/Onsalegoods";
    }



    @RequestMapping(value = "/Deleteuser", method = RequestMethod.GET)
    public String deleteuser(String userpk) {
        User user = new User();
        user.setUserpk(Integer.parseInt(userpk));
        userService.delete(user);
        return "redirect:/Usertable";
    }

    // 更新用户函数
    @RequestMapping(value = "/Saveuser", method = RequestMethod.POST)
    public String saveuser(User user) {
        userService.save(user);
        return "redirect:/Usertable";
    }

    // 添加用户的按钮的函数
    @PostMapping(value = "/Adduser")
    public String adduser(User user)  {
        userService.insert(user);
        return "redirect:/Usertable";
    }
}
